package com.easybenefit.UUService.activity;

import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Paint;
import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.ToggleButton;

import com.easybeanfit.UUService.commutill.DensityUtil;
import com.easybeanfit.UUService.commutill.UUDialogUtill;
import com.easybeanfit.UUService.commutill.UUJsonUtil;
import com.easybeanfit.UUService.commutill.UULogUtil;
import com.easybeanfit.UUService.commutill.UUNetUtill;
import com.easybeanfit.UUService.commutill.UUSharedUtil;
import com.easybeanfit.UUService.commutill.UUStrUtil;
import com.easybeanfit.UUService.commutill.UUToastUtil;
import com.easybeanfit.UUService.config.UUConfig;
import com.easybeanfit.UUService.config.UUDict;
import com.easybenefit.UUService.BaseActivity;
import com.easybenefit.UUService.R;
import com.easybenefit.UUService.adapter.MutilSelectListAdapter;
import com.easybenefit.UUService.adapter.SelectListAdapter;
import com.easybenefit.UUService.model.HttpCouponBean;
import com.easybenefit.UUService.model.HttpStoreBean;
import com.easybenefit.UUService.model.PopBean;
import com.easybenefit.UUService.model.UserInfo;
import com.easybenefit.UUService.myinterface.IActivityInterface;
import com.easybenefit.UUService.wight.WheelView;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;
import org.apache.http.message.BasicNameValuePair;

import java.lang.reflect.Array;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;

public class CouponActivity extends BaseActivity implements IActivityInterface, View.OnClickListener {
    private Button btn_back, btn_add;//返回和发布
    private TextView tv_title;
    private EditText et_coupons_name;//优惠券名称
    private EditText et_coupons_money;//优惠券金额
    private TextView tv_et_explain_length;//说明长度显示值
    private TextView tv_et_digest_length;//摘要长度显示值
    private TextView tv_coupon_type_value;//抵值优惠券
    private TextView tv_coupon_type;//低值券类型
    private TextView tv_unit;//低值券单位
    private TextView tv_store;//适用门店
    private EditText et_digest;//摘要
    private EditText et_explain;//说明使用规则
    private EditText et_start_time;//开始时间
    private EditText et_end_time;//结束时间
    private ToggleButton tgb_distributed;//是否派发
    private ToggleButton tgb_union_distributed;//是否联盟派发
    private int max_digest_length = 30;//最大长度
    private int max_explain_length = 500;//最大长度
    private PopupWindow popupWindow;//弹出窗
    private ListView listView;//弹出框的listview
    private WheelView wheelView;//弹出框的滚动选择器
    private List list;//抵值优惠券或者门店数据
    private Context context;
    private SelectListAdapter selectListAdapter;//选择列表的adapter 点击弹出时候就需要初始化
    private MutilSelectListAdapter mutilSelectListAdapter;//门店选择的adapter
    private int year;
    private int month;
    private int day;
    private int hours;
    private int minutes;
    private int second;
    private ProgressDialog progressDialog;
    private TextView tv_validity_time;
    private SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm");
    private DatePicker datePicker;
    private TimePicker timePicker;
    private boolean isinited = false;//时间窗口是否被初始化
    private List<PopBean> couponTypeList;//优惠券类型列表
    private List<String> couponTypeliststr;//优惠券类型list<string>
    private List<PopBean> storeList;//门店list
    private int record_coupon_type_index = 0;//低值券类型索引
    private int temp_index = 0;
    private int record_store_index = 0;//门店索引
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    private int count = 0;
    private List<Boolean> booleanList = new ArrayList<>();
    private LinearLayout llt_coupontype;//低值券类型linearlayout
    private LinearLayout llt_validity_time;//有效期
    private LinearLayout llt_store;//门店

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_coupon);
        initView();
        initData();
        initListener();
    }

    @Override
    public void initData() {
        context = this;
        tv_title.setText(getResources().getText(R.string.coupons_add_title));
        tv_validity_time.setText(format.format(new Date()));
        couponTypeList = new ArrayList<>();
        couponTypeliststr = new ArrayList<>();
        storeList = new ArrayList<>();
        PopBean popBean1 = new PopBean();
        popBean1.setId("6");
        popBean1.setName("抵值优惠券");
        couponTypeliststr.add(popBean1.getName());
        couponTypeList.add(popBean1);
        PopBean popBean2 = new PopBean();
        popBean2.setId("8");
        popBean2.setName("折扣优惠券");
        couponTypeliststr.add(popBean2.getName());
        couponTypeList.add(popBean2);
        PopBean popBean3 = new PopBean();
        popBean3.setId("7");
        popBean3.setName("自定义优惠券");
        couponTypeliststr.add(popBean3.getName());
        couponTypeList.add(popBean3);

        getStoreData();
        tv_coupon_type_value.setText(couponTypeList.get(record_coupon_type_index).getName());
        tv_store.setText("所有门店");
        tgb_distributed.setChecked(true);
    }

    @Override
    public void initView() {
        btn_back = (Button) findViewById(R.id.finish_btn);
        tv_title = (TextView) findViewById(R.id.top_title);
        btn_add = (Button) findViewById(R.id.btn_add);
        et_coupons_name = (EditText) findViewById(R.id.et_coupon_name);
        et_coupons_money = (EditText) findViewById(R.id.et_coupon_money);
        et_coupons_money.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        llt_coupontype = (LinearLayout) findViewById(R.id.llt_coupontype);
        tv_et_explain_length = (TextView) findViewById(R.id.tv_et_explain_length);
        tv_et_digest_length = (TextView) findViewById(R.id.tv_et_digest_length);
        tv_coupon_type_value = (TextView) findViewById(R.id.tv_coupon_type_value);
        llt_store = (LinearLayout) findViewById(R.id.llt_store);
        llt_validity_time = (LinearLayout) findViewById(R.id.llt_validity_time);
        tv_store = (TextView) findViewById(R.id.tv_store);
        et_digest = (EditText) findViewById(R.id.et_digest);
        et_explain = (EditText) findViewById(R.id.et_explain);
        tgb_distributed = (ToggleButton) findViewById(R.id.tgb_distributed);
        tgb_union_distributed = (ToggleButton) findViewById(R.id.tgb_union_distributed);
        tv_validity_time = (TextView) findViewById(R.id.tv_validity_time);
        tv_coupon_type = (TextView) findViewById(R.id.tv_coupon_type);
        tv_unit = (TextView) findViewById(R.id.tv_unit);
    }

    @Override
    public void initListener() {
        et_digest.addTextChangedListener(digestWatcher);
        et_explain.addTextChangedListener(explainWatcher);
        btn_back.setOnClickListener(this);
        btn_add.setOnClickListener(this);
        //tv_store.setOnClickListener(this);
        //tv_coupon_type_value.setOnClickListener(this);
        llt_coupontype.setOnClickListener(this);
        //tv_validity_time.setOnClickListener(this);
        llt_store.setOnClickListener(this);
        llt_validity_time.setOnClickListener(this);
        tgb_distributed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (tgb_distributed.isChecked()) {

                } else {
                    tgb_union_distributed.setChecked(false);
                }
            }
        });
    }

    //摘要文本监听
    private TextWatcher digestWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            if (et_digest.length() > max_digest_length) {
                et_digest.setText(et_digest.getText().toString().substring(0, max_digest_length));
                tv_et_digest_length.setTextColor(getResources().getColor(R.color.bigred));
                et_digest.setSelection(max_digest_length);
            }
            tv_et_digest_length.setText("还可输入" + (max_digest_length - et_digest.length()) + "字");
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };
    //说明文本监听
    private TextWatcher explainWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            if (et_explain.length() > max_explain_length) {
                et_explain.setText(et_explain.getText().toString().substring(0, max_explain_length));
                et_explain.setSelection(max_explain_length);
                tv_et_explain_length.setTextColor(getResources().getColor(R.color.bigred));
            }
            tv_et_explain_length.setText("还可输入" + (max_explain_length - et_explain.length()) + "字");
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.finish_btn:
                finish();
                break;
            case R.id.btn_add:
                submitCouponData();
                break;
            case R.id.llt_validity_time:
                tv_validity_time.setFocusable(true);
                tv_validity_time.requestFocus();
                showDialogWindow(v);
                break;
            case R.id.llt_store:
                //这里需要初始化数据
                tv_store.setFocusable(true);
                tv_store.requestFocus();
                mutilSelectListAdapter = new MutilSelectListAdapter(storeList, context, booleanList);
                showStorePopupWindow(v, storeList);
                break;
            case R.id.llt_coupontype:
                //这里需要初始化数据
                tv_coupon_type_value.setFocusable(true);
                tv_coupon_type_value.requestFocus();
                selectListAdapter = new SelectListAdapter(couponTypeList, context);
                showCouponPopupWindow(v, couponTypeList);
                break;

        }
    }

    /**
     * 低值券的popwindow
     *
     * @param mainview
     * @param popBeanList
     */
    private void showCouponPopupWindow(final View mainview, final List<PopBean> popBeanList) {
        hidwindow();
        // 一个自定义的布局，作为显示的内容
        View contentView = LayoutInflater.from(context).inflate(
                R.layout.popwoindow, null);
        // 设置按钮的点击事件
        popupWindow = new PopupWindow(contentView,
                LinearLayout.LayoutParams.MATCH_PARENT, DensityUtil.dip2px(context, 260), true);
//        listView = (ListView) contentView.findViewById(R.id.pop_listview);
//        listView.setAdapter(selectListAdapter);
        wheelView = (WheelView) contentView.findViewById(R.id.wheelview);
        //selectListAdapter.notifyDataSetChanged();
        wheelView.setOffset(1);
        wheelView.setSeletion(record_coupon_type_index);
        wheelView.setItems(couponTypeliststr);
        wheelView.setOnWheelViewListener(new WheelView.OnWheelViewListener() {
            @Override
            public void onSelected(int selectedIndex, String item) {
                temp_index = selectedIndex - 1;
            }
        });
        popupWindow.setTouchable(true);
        popupWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.i("mengdd", "onTouch : ");
                return false;
                // 这里如果返回true的话，touch事件将被拦截
                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
            }
        });
        // 如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框
        // 我觉得这里是API的一个bug
        popupWindow.setOutsideTouchable(false);
        popupWindow.setBackgroundDrawable(getResources().getDrawable(
                R.color.white));
        popupWindow.showAtLocation(mainview,
                Gravity.BOTTOM, 0, 0);
        // 设置好参数之后再show
        Button btn_ok = (Button) contentView.findViewById(R.id.btn_ok);
        Button btn_cancel = (Button) contentView.findViewById(R.id.btn_cancel);
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        btn_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                record_coupon_type_index = temp_index;
                switch (popBeanList.get(record_coupon_type_index).getId()) {
                    case "6":
                        tv_coupon_type.setText("代金券");
                        tv_unit.setText("元");
                        tv_coupon_type_value.setText(popBeanList.get(record_coupon_type_index).getName());
                        tv_unit.setVisibility(View.VISIBLE);
                        et_coupons_money.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
                        break;
                    case "8":
                        tv_coupon_type.setText("折扣券");
                        tv_unit.setText("折");
                        tv_coupon_type_value.setText(popBeanList.get(record_coupon_type_index).getName());
                        et_coupons_money.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
                        tv_unit.setVisibility(View.VISIBLE);
                        break;
                    case "7":
                        tv_coupon_type.setText("自定义券");
                        tv_unit.setText(" ");
                        tv_coupon_type_value.setText(popBeanList.get(record_coupon_type_index).getName());
                        et_coupons_money.setInputType(InputType.TYPE_CLASS_TEXT);
                        tv_unit.setVisibility(View.INVISIBLE);
                        break;
                }
                popupWindow.dismiss();
            }

        });

    }

    /**
     * 门店的popwindow
     *
     * @param mainview
     * @param popBeanList
     */
    private void showStorePopupWindow(final View mainview, final List<PopBean> popBeanList) {
        hidwindow();
        // 一个自定义的布局，作为显示的内容
        View contentView = LayoutInflater.from(context).inflate(
                R.layout.pop_store_window, null);
        // 设置按钮的点击事件
        popupWindow = new PopupWindow(contentView,
                LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT, true);
//        if (isfirstopen) {
        listView = (ListView) contentView.findViewById(R.id.pop_listview);
        Button btn_close = (Button) contentView.findViewById(R.id.finish_btn);
        Button btn_save = (Button) contentView.findViewById(R.id.btn_add);
        btn_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setting_show_textview(tv_store);

            }
        });
        listView.setAdapter(mutilSelectListAdapter);

        popupWindow.setTouchable(true);
        popupWindow.setTouchInterceptor(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Log.i("mengdd", "onTouch : ");
                return false;
                // 这里如果返回true的话，touch事件将被拦截
                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
            }
        });
        // 如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框
        // 我觉得这里是API的一个bug
        popupWindow.setBackgroundDrawable(getResources().getDrawable(
                R.color.white));
        popupWindow.showAtLocation(mainview,
                Gravity.BOTTOM, 0, 0);
    }

    /**
     * 设置显示门店是所有门店还是部分门店
     */
    private void setting_show_textview(View view) {
        count = 0;
        for (int i = 0; i < booleanList.size(); i++) {
            if (booleanList.get(i)) {
                count++;
                storeList.get(i).setIschecked(true);
            } else {
                storeList.get(i).setIschecked(false);
            }
        }
        if (count == booleanList.size()) {
            ((TextView) view).setText("所有门店");
        } else if (count > 0) {
            ((TextView) view).setText("部分门店");
        } else if (count == 0) {
            UUToastUtil.showToast(context, "请至少选则一个门店");
            return;
        }
        popupWindow.dismiss();
    }

    //弹出时间popupwindow
    private void showDialogWindow(final View view) {
        hidwindow();

        // 一个自定义的布局，作为显示的内容
        View contentView = LayoutInflater.from(context).inflate(
                R.layout.dialog_time, null);
        // 设置按钮的点击事件
        popupWindow = new PopupWindow(contentView,
                LinearLayout.LayoutParams.MATCH_PARENT, DensityUtil.dip2px(context, 360), true);
        popupWindow.setTouchable(true);

        popupWindow.setTouchInterceptor(new View.OnTouchListener() {

            @Override
            public boolean onTouch(View v, MotionEvent event) {

                Log.i("mengdd", "onTouch : ");
                return false;
                // 这里如果返回true的话，touch事件将被拦截
                // 拦截后 PopupWindow的onTouchEvent不被调用，这样点击外部区域无法dismiss
            }
        });

        // 如果不设置PopupWindow的背景，无论是点击外部区域还是Back键都无法dismiss弹框
        // 我觉得这里是API的一个bug
        popupWindow.setBackgroundDrawable(getResources().getDrawable(
                R.color.white));
       /* popupWindow.showAtLocation(view,
                Gravity.BOTTOM, 0, 0);*/
        // 设置好参数之后再show

        datePicker = (DatePicker) contentView.findViewById(R.id.datePicker);
        timePicker = (TimePicker) contentView.findViewById(R.id.timePicker);
        Button btn_ok = (Button) contentView.findViewById(R.id.btn_ok);
        Button btn_cancel = (Button) contentView.findViewById(R.id.btn_cancel);
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                popupWindow.dismiss();
            }
        });
        btn_ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                year = datePicker.getYear();
                month = datePicker.getMonth() + 1;
                day = datePicker.getDayOfMonth();
                hours = timePicker.getCurrentHour();
                minutes = timePicker.getCurrentMinute();
                String monthStr = "";
                if ((month + "").length() < 2) {
                    monthStr = "0" + month;
                } else {
                    monthStr = month + "";
                }
                String dayStr = "";
                if ((day + "").length() < 2) {
                    dayStr = "0" + day;
                } else {
                    dayStr = day + "";
                }
                String hoursStr = "";
                if ((hours + "").length() < 2) {
                    hoursStr = "0" + hours;
                } else {
                    hoursStr = hours + "";
                }
                String minutesStr = "";
                if ((minutes + "").length() < 2) {
                    minutesStr = "0" + minutes;
                } else {
                    minutesStr = minutes + "";
                }
                tv_validity_time.setText(year + "-" + monthStr + "-" +
                        dayStr + " " + hoursStr + ":" + minutesStr);
                popupWindow.dismiss();
            }
        });
        timePicker.setIs24HourView(true);


        if (isinited) {
            datePicker.init(year, month - 1, day, new DatePicker.OnDateChangedListener() {
                @Override
                public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

                }
            });
            timePicker.setCurrentHour(hours);
            timePicker.setCurrentMinute(minutes);
        }
        isinited = true;
        popupWindow.showAsDropDown(view);
    }


    /**
     * 发布优惠券
     */
    public void submitCouponData() {
        if (!UUNetUtill.isConnected(context)) {
            UUToastUtil.showToast(context, UUDict.NOT_INTERNET);
        } else {
            UserInfo userInfo = (UserInfo) UUSharedUtil.getObject(context, UUConfig.UU_USERINFO_NAME, UserInfo.class);
            /** 系统商户序号 */
            String mer_id = userInfo.getMer_id();
            /** 2 */
            String belong_id = "2";
            /** 标题 */
            String coupon_name = et_coupons_name.getText().toString().trim();
            if (UUStrUtil.isEmpty(coupon_name)) {
                UUToastUtil.showToast(context, "请输入优惠券名称");
                return;
            }
            /** 摘要 */
            String coupon_sum = et_digest.getText().toString().trim();
            if (UUStrUtil.isEmpty(coupon_sum)) {
                UUToastUtil.showToast(context, "请输入推荐理由");
                return;
            }
            /** 使用规则 */
            String coupon_rules = et_explain.getText().toString().trim();
            if (UUStrUtil.isEmpty(coupon_rules)) {
                UUToastUtil.showToast(context, "请输入使用规则");
                return;
            }
            /** 类型 */
            String coupon_type = tv_coupon_type_value.getText().toString().trim();
            /** 类型值内容 */
            String coupon_price = et_coupons_money.getText().toString().trim();
            /**  有效期开始日期*/
            Date startDate = new Date();
            String starttime = sdf.format(startDate);
            /** 有效期开始小时 */
            String starthour = "" + startDate.getHours();
            /**  有效期开始分*/
            String startminute = "" + startDate.getMinutes();
            /** 有效期结束日期 */
            String endtime = tv_validity_time.getText().toString().substring(0, 10);
            /** 有效期结束小时 */
            String endhour = "";
            if ((hours + "").length() < 2) {
                endhour = "0" + hours;
            } else {
                endhour = hours + "";
            }
            /** 有效期结束分 */
            String endminute = "";
            if ((minutes + "").length() < 2) {
                endminute = "0" + minutes;
            } else {
                endminute = minutes + "";
            }
            /** 是否允许派发1：允许，0：不允许 */
            String coupon_distr = "";
            /** 是否允许联盟派发1：允许，0：不允许 */
            String coupon_union = "";
            if (tgb_distributed.isChecked()) {//如果不允许派发那么更不允许联盟派发
                coupon_distr = "1";
                if (tgb_union_distributed.isChecked()) {
                    coupon_union = "1";
                } else {
                    coupon_union = "0";
                }
            } else {
                coupon_distr = "0";
                coupon_union = "0";
            }


            try {
                Date endDate = sdf.parse(tv_validity_time.getText().toString());
                if (endDate.getTime() - startDate.getTime() <= 0) {
                    UUToastUtil.showToast(context, "有效期时间应在当前时间之后");
                    return;
                }
                if (endDate.getTime() - sdf.parse(userInfo.getEnd_date()).getTime() > 0) {
                    UUToastUtil.showToast(context, "您的服务有效期截至到" + userInfo.getEnd_date());
                    return;
                }
            } catch (ParseException e) {
                e.printStackTrace();
            }

            List<String> store_id = new LinkedList<>();
            for (int i = 0; i < storeList.size(); i++) {
                if (storeList.get(i).ischecked()) {
                    store_id.add(storeList.get(i).getId());
                }

            }
            String url = UUConfig.SERVER_BASE_URL + "newcouadd";
            RequestParams params = new RequestParams();
            params.put("mer_id", mer_id);
            params.put("user_id", userInfo.getUser_id());
            params.put("belong_id", "2");
            params.put("coupon_name", coupon_name);
            params.put("coupon_sum", coupon_sum);
            params.put("coupon_rules", coupon_rules);
            params.put("coupon_type", couponTypeList.get(record_coupon_type_index).getId());
            params.put("coupon_price", coupon_price);
            params.put("starttime", starttime);
            params.put("starthour", starthour);
            params.put("startminute", startminute);
            params.put("endtime", endtime);
            params.put("endhour", endhour);
            params.put("endminute", endminute);

            params.put("store_id", store_id);
            params.put("coupon_distr", coupon_distr);
            params.put("coupon_union", coupon_union);
            AsyncHttpClient client = new AsyncHttpClient();

            client.post(url, params, new AsyncHttpResponseHandler() {
                @Override
                public void onStart() {
                    progressDialog = UUDialogUtill.getLoadingBar(context, "正在添加请稍后...");
                    progressDialog.show();
                }

                @Override
                public void onFinish() {
                    progressDialog.dismiss();
                }

                @Override
                public void onSuccess(int i, Header[] headers, byte[] bytes) {
                    String content = new String(bytes);
                    HttpCouponBean repon = (HttpCouponBean) UUJsonUtil.fromJson(content, HttpCouponBean.class);
                    UUToastUtil.showToast(context, repon.getMessage());
                    CouponActivity.this.finish();
                }

                @Override
                public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                    String info = new String(bytes);
                    UULogUtil.e("onFailure", info);
                }
            });
        }
    }

    /**
     * 获取门店数据
     */
    public void getStoreData() {

        if (!UUNetUtill.isConnected(context)) {
            UUToastUtil.showToast(context, UUDict.NOT_INTERNET);
        } else {
            /** 获取优惠券列表 */
            String url = UUConfig.SERVER_BASE_URL + "storelist";
            RequestParams params = new RequestParams();
            UserInfo userInfo = (UserInfo) UUSharedUtil.getObject(context, UUConfig.UU_USERINFO_NAME, UserInfo.class);
            params.put("mer_id", userInfo.getMer_id());
            AsyncHttpClient client = new AsyncHttpClient();
            client.post(url, params, new AsyncHttpResponseHandler() {
                @Override
                public void onStart() {
                    progressDialog = UUDialogUtill.getLoadingBar(context, "正在获取请稍后...");
                    progressDialog.show();
                }

                @Override
                public void onFinish() {
                    progressDialog.dismiss();
                }

                @Override
                public void onSuccess(int i, Header[] headers, byte[] bytes) {
                    String content = new String(bytes);
                    HttpStoreBean httpCouponBean = (HttpStoreBean) UUJsonUtil.fromJson(content, HttpStoreBean.class);
//                    list.addAll(httpCouponBean.getData().getCouponlist());
                    for (HttpStoreBean.DataBean.StorelistBean storelistBean : httpCouponBean.getData().getStorelist()) {
                        PopBean popBean = new PopBean();
                        popBean.setId(storelistBean.getStore_id());
                        popBean.setName(storelistBean.getStore_name());
                        popBean.setIschecked(true);
                        storeList.add(popBean);
                        booleanList.add(true);
                    }

                }

                @Override
                public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {

                }
            });

        }
    }

    /**
     * 隐藏键盘
     */
    public void hidwindow() {
        InputMethodManager im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);

        im.hideSoftInputFromWindow(getCurrentFocus().getApplicationWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }
}
